package com.clothing.customization.controller;

import com.clothing.customization.common.ApiResponse;
import com.clothing.customization.common.PageResult;
import com.clothing.customization.entity.ClothingMaterial;
import com.clothing.customization.service.ClothingMaterialService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

@RestController
@RequestMapping("/clothing/material")
@Tag(name = "服装材质管理", description = "服装材质增删改查接口")
public class ClothingMaterialController {

    @Autowired
    private ClothingMaterialService clothingMaterialService;

    @GetMapping("/list")
    @Operation(summary = "获取材质列表", description = "分页获取材质列表，支持按名称和状态筛选")
    public ApiResponse<PageResult<ClothingMaterial>> list(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "材质名称") @RequestParam(required = false) String name,
            @Parameter(description = "状态(1-启用 0-禁用)") @RequestParam(required = false) Integer status
    ) {
        PageResult<ClothingMaterial> result = clothingMaterialService.getMaterialList(pageNum, pageSize, name, status);
        return ApiResponse.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取材质详情", description = "根据ID获取材质详情")
    public ApiResponse<ClothingMaterial> getById(@PathVariable Long id) {
        ClothingMaterial material = clothingMaterialService.getById(id);
        return ApiResponse.success(material);
    }

    @PostMapping
    @Operation(summary = "添加材质", description = "添加新的服装材质")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ApiResponse<ClothingMaterial> add(@Valid @RequestBody ClothingMaterial material) {
        ClothingMaterial result = clothingMaterialService.addMaterial(material);
        return ApiResponse.success(result);
    }

    @PutMapping
    @Operation(summary = "更新材质", description = "更新已有的服装材质")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ApiResponse<ClothingMaterial> update(@Valid @RequestBody ClothingMaterial material) {
        ClothingMaterial result = clothingMaterialService.updateMaterial(material);
        return ApiResponse.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除材质", description = "根据ID删除服装材质")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ApiResponse<Boolean> delete(@PathVariable Long id) {
        boolean result = clothingMaterialService.deleteMaterial(id);
        return ApiResponse.success(result);
    }

    @PutMapping("/status")
    @Operation(summary = "更新材质状态", description = "启用或禁用材质")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ApiResponse<Boolean> updateStatus(
            @Parameter(description = "材质ID") @RequestParam Long id,
            @Parameter(description = "状态(1-启用 0-禁用)") @RequestParam Integer status
    ) {
        boolean result = clothingMaterialService.updateStatus(id, status);
        return ApiResponse.success(result);
    }
} 